;;;; bit-operations.test --- bitwise operations on numbers -*- scheme -*-
;;;; Copyright (C) 2000, 2001 Free Software Foundation, Inc.
;;;;
;;;; This program is free software; you can redistribute it and/or modify
;;;; it under the terms of the GNU General Public License as published by
;;;; the Free Software Foundation; either version 2, or (at your option)
;;;; any later version.
;;;;
;;;; This program is distributed in the hope that it will be useful,
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;;; GNU General Public License for more details.
;;;;
;;;; You should have received a copy of the GNU General Public License
;;;; along with this software; see the file COPYING.  If not, write to
;;;; the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
;;;; Boston, MA 02111-1307 USA
;;;;
;;;; As a special exception, the Free Software Foundation gives permission
;;;; for additional uses of the text contained in its release of GUILE.
;;;;
;;;; The exception is that, if you link the GUILE library with other files
;;;; to produce an executable, this does not by itself cause the
;;;; resulting executable to be covered by the GNU General Public License.
;;;; Your use of that executable is in no way restricted on account of
;;;; linking the GUILE library code into it.
;;;;
;;;; This exception does not however invalidate any other reasons why
;;;; the executable file might be covered by the GNU General Public License.
;;;;
;;;; This exception applies only to the code released by the
;;;; Free Software Foundation under the name GUILE.  If you copy
;;;; code from other Free Software Foundation releases into a copy of
;;;; GUILE, as the General Public License permits, the exception does
;;;; not apply to the code that you add in this way.  To avoid misleading
;;;; anyone as to the status of such modified files, you must delete
;;;; this exception notice from them.
;;;;
;;;; If you write modifications of your own for GUILE, it is your choice
;;;; whether to permit this exception to apply to your modifications.
;;;; If you do not wish that, delete this exception notice.

(use-modules (ice-9 documentation))


;;;
;;; miscellaneous
;;;

(define (run-tests name-proc test-proc arg-sets)
  (for-each
   (lambda (arg-set)
     (pass-if (apply name-proc arg-set)
       (apply test-proc arg-set)))
   arg-sets))

(define (documented? object)
  (not (not (object-documentation object))))

(define fixnum-bit
  (inexact->exact (+ (/ (log (+ most-positive-fixnum 1)) (log 2)) 1)))

(define fixnum-min most-negative-fixnum)
(define fixnum-max most-positive-fixnum)

(with-test-prefix "bit-extract"

  (pass-if "documented?"
    (documented? bit-extract))

  (with-test-prefix "extract from zero"

    (run-tests
     (lambda (a b c d)
       (string-append "single bit " (number->string b)))
     (lambda (a b c d)
       (= (bit-extract a b c) d))
     (list
      (list 0 0 1 0)
      (list 0 1 2 0)
      (list 0 (+ fixnum-bit -2) (+ fixnum-bit -1) 0)
      (list 0 (+ fixnum-bit -1) (+ fixnum-bit  0) 0)
      (list 0 (+ fixnum-bit  0) (+ fixnum-bit  1) 0)
      (list 0 (+ fixnum-bit  1) (+ fixnum-bit  2) 0)))

    (run-tests
     (lambda (a b c d)
       (string-append "fixnum-bit - 1 bits starting at " (number->string b)))
     (lambda (a b c d)
       (= (bit-extract a b c) d))
     (list
      (list 0 0 (+ fixnum-bit -1) 0)
      (list 0 1 (+ fixnum-bit  0) 0)
      (list 0 2 (+ fixnum-bit  1) 0)
      (list 0 (+ fixnum-bit -2) (+ fixnum-bit fixnum-bit -3) 0)
      (list 0 (+ fixnum-bit -1) (+ fixnum-bit fixnum-bit -2) 0)
      (list 0 (+ fixnum-bit  0) (+ fixnum-bit fixnum-bit -1) 0)
      (list 0 (+ fixnum-bit  1) (+ fixnum-bit fixnum-bit  0) 0)))

    (run-tests
     (lambda (a b c d)
       (string-append "fixnum-bit bits starting at " (number->string b)))
     (lambda (a b c d)
       (= (bit-extract a b c) d))
     (list
      (list 0 0 (+ fixnum-bit  0) 0)
      (list 0 1 (+ fixnum-bit  1) 0)
      (list 0 2 (+ fixnum-bit  2) 0)
      (list 0 (+ fixnum-bit -2) (+ fixnum-bit fixnum-bit -2) 0)
      (list 0 (+ fixnum-bit -1) (+ fixnum-bit fixnum-bit -1) 0)
      (list 0 (+ fixnum-bit  0) (+ fixnum-bit fixnum-bit  0) 0)
      (list 0 (+ fixnum-bit  1) (+ fixnum-bit fixnum-bit  1) 0)))

    (run-tests
     (lambda (a b c d)
       (string-append "fixnum-bit + 1 bits starting at " (number->string b)))
     (lambda (a b c d)
       (= (bit-extract a b c) d))
     (list
      (list 0 0 (+ fixnum-bit  1) 0)
      (list 0 1 (+ fixnum-bit  2) 0)
      (list 0 2 (+ fixnum-bit  3) 0)
      (list 0 (+ fixnum-bit -2) (+ fixnum-bit fixnum-bit -1) 0)
      (list 0 (+ fixnum-bit -1) (+ fixnum-bit fixnum-bit  0) 0)
      (list 0 (+ fixnum-bit  0) (+ fixnum-bit fixnum-bit  1) 0)
      (list 0 (+ fixnum-bit  1) (+ fixnum-bit fixnum-bit  2) 0))))

  (with-test-prefix "extract from fixnum-max"

    (run-tests
     (lambda (a b c d)
       (string-append "single bit " (number->string b)))
     (lambda (a b c d)
       (= (bit-extract a b c) d))
     (list
      (list fixnum-max 0 1 1)
      (list fixnum-max 1 2 1)
      (list fixnum-max (+ fixnum-bit -2) (+ fixnum-bit -1) 1)
      (list fixnum-max (+ fixnum-bit -1) (+ fixnum-bit  0) 0)
      (list fixnum-max (+ fixnum-bit  0) (+ fixnum-bit  1) 0)
      (list fixnum-max (+ fixnum-bit  1) (+ fixnum-bit  2) 0)))

    (run-tests
     (lambda (a b c d)
       (string-append "fixnum-bit - 1 bits starting at " (number->string b)))
     (lambda (a b c d)
       (= (bit-extract a b c) d))
     (list
      (list fixnum-max 0 (+ fixnum-bit -1) (ash fixnum-max  0))
      (list fixnum-max 1 (+ fixnum-bit  0) (ash fixnum-max -1))
      (list fixnum-max 2 (+ fixnum-bit  1) (ash fixnum-max -2))
      (list fixnum-max (+ fixnum-bit -2) (+ fixnum-bit fixnum-bit -3) 1)
      (list fixnum-max (+ fixnum-bit -1) (+ fixnum-bit fixnum-bit -2) 0)
      (list fixnum-max (+ fixnum-bit  0) (+ fixnum-bit fixnum-bit -1) 0)
      (list fixnum-max (+ fixnum-bit  1) (+ fixnum-bit fixnum-bit  0) 0)))

    (run-tests
     (lambda (a b c d)
       (string-append "fixnum-bit bits starting at " (number->string b)))
     (lambda (a b c d)
       (= (bit-extract a b c) d))
     (list
      (list fixnum-max 0 (+ fixnum-bit  0) (ash fixnum-max  0))
      (list fixnum-max 1 (+ fixnum-bit  1) (ash fixnum-max -1))
      (list fixnum-max 2 (+ fixnum-bit  2) (ash fixnum-max -2))
      (list fixnum-max (+ fixnum-bit -2) (+ fixnum-bit fixnum-bit -2) 1)
      (list fixnum-max (+ fixnum-bit -1) (+ fixnum-bit fixnum-bit -1) 0)
      (list fixnum-max (+ fixnum-bit  0) (+ fixnum-bit fixnum-bit  0) 0)
      (list fixnum-max (+ fixnum-bit  1) (+ fixnum-bit fixnum-bit  1) 0)))

    (run-tests
     (lambda (a b c d)
       (string-append "fixnum-bit + 1 bits starting at " (number->string b)))
     (lambda (a b c d)
       (= (bit-extract a b c) d))
     (list
      (list fixnum-max 0 (+ fixnum-bit  1) (ash fixnum-max  0))
      (list fixnum-max 1 (+ fixnum-bit  2) (ash fixnum-max -1))
      (list fixnum-max 2 (+ fixnum-bit  3) (ash fixnum-max -2))
      (list fixnum-max (+ fixnum-bit -2) (+ fixnum-bit fixnum-bit -1) 1)
      (list fixnum-max (+ fixnum-bit -1) (+ fixnum-bit fixnum-bit  0) 0)
      (list fixnum-max (+ fixnum-bit  0) (+ fixnum-bit fixnum-bit  1) 0)
      (list fixnum-max (+ fixnum-bit  1) (+ fixnum-bit fixnum-bit  2) 0))))

  (with-test-prefix "extract from fixnum-max + 1"

    (run-tests
     (lambda (a b c d)
       (string-append "single bit " (number->string b)))
     (lambda (a b c d)
       (= (bit-extract a b c) d))
     (list
      (list (+ fixnum-max 1) 0 1 0)
      (list (+ fixnum-max 1) 1 2 0)
      (list (+ fixnum-max 1) (+ fixnum-bit -2) (+ fixnum-bit -1) 0)
      (list (+ fixnum-max 1) (+ fixnum-bit -1) (+ fixnum-bit  0) 1)
      (list (+ fixnum-max 1) (+ fixnum-bit  0) (+ fixnum-bit  1) 0)
      (list (+ fixnum-max 1) (+ fixnum-bit  1) (+ fixnum-bit  2) 0)))

    (run-tests
     (lambda (a b c d)
       (string-append "fixnum-bit - 1 bits starting at " (number->string b)))
     (lambda (a b c d)
       (= (bit-extract a b c) d))
     (list
      (list (+ fixnum-max 1) 0 (+ fixnum-bit -1) (ash 0 (- fixnum-bit 1)))
      (list (+ fixnum-max 1) 1 (+ fixnum-bit  0) (ash 1 (- fixnum-bit 2)))
      (list (+ fixnum-max 1) 2 (+ fixnum-bit  1) (ash 1 (- fixnum-bit 3)))
      (list (+ fixnum-max 1) (+ fixnum-bit -2) (+ fixnum-bit fixnum-bit -3) 2)
      (list (+ fixnum-max 1) (+ fixnum-bit -1) (+ fixnum-bit fixnum-bit -2) 1)
      (list (+ fixnum-max 1) (+ fixnum-bit  0) (+ fixnum-bit fixnum-bit -1) 0)
      (list (+ fixnum-max 1) (+ fixnum-bit  1) (+ fixnum-bit fixnum-bit  0) 0)))

    (run-tests
     (lambda (a b c d)
       (string-append "fixnum-bit bits starting at " (number->string b)))
     (lambda (a b c d)
       (= (bit-extract a b c) d))
     (list
      (list (+ fixnum-max 1) 0 (+ fixnum-bit  0) (ash 1 (- fixnum-bit 1)))
      (list (+ fixnum-max 1) 1 (+ fixnum-bit  1) (ash 1 (- fixnum-bit 2)))
      (list (+ fixnum-max 1) 2 (+ fixnum-bit  2) (ash 1 (- fixnum-bit 3)))
      (list (+ fixnum-max 1) (+ fixnum-bit -2) (+ fixnum-bit fixnum-bit -2) 2)
      (list (+ fixnum-max 1) (+ fixnum-bit -1) (+ fixnum-bit fixnum-bit -1) 1)
      (list (+ fixnum-max 1) (+ fixnum-bit  0) (+ fixnum-bit fixnum-bit  0) 0)
      (list (+ fixnum-max 1) (+ fixnum-bit  1) (+ fixnum-bit fixnum-bit  1) 0)))

    (run-tests
     (lambda (a b c d)
       (string-append "fixnum-bit + 1 bits starting at " (number->string b)))
     (lambda (a b c d)
       (= (bit-extract a b c) d))
     (list
      (list (+ fixnum-max 1) 0 (+ fixnum-bit  1) (ash 1 (- fixnum-bit 1)))
      (list (+ fixnum-max 1) 1 (+ fixnum-bit  2) (ash 1 (- fixnum-bit 2)))
      (list (+ fixnum-max 1) 2 (+ fixnum-bit  3) (ash 1 (- fixnum-bit 3)))
      (list (+ fixnum-max 1) (+ fixnum-bit -2) (+ fixnum-bit fixnum-bit -1) 2)
      (list (+ fixnum-max 1) (+ fixnum-bit -1) (+ fixnum-bit fixnum-bit  0) 1)
      (list (+ fixnum-max 1) (+ fixnum-bit  0) (+ fixnum-bit fixnum-bit  1) 0)
      (list (+ fixnum-max 1) (+ fixnum-bit  1) (+ fixnum-bit fixnum-bit  2) 0))))

  (with-test-prefix "extract from fixnum-min"

    (run-tests
     (lambda (a b c d)
       (string-append "single bit " (number->string b)))
     (lambda (a b c d)
       (= (bit-extract a b c) d))
     (list
      (list fixnum-min 0 1 0)
      (list fixnum-min 1 2 0)
      (list fixnum-min (+ fixnum-bit -2) (+ fixnum-bit -1) 0)
      (list fixnum-min (+ fixnum-bit -1) (+ fixnum-bit  0) 1)
      (list fixnum-min (+ fixnum-bit  0) (+ fixnum-bit  1) 1)
      (list fixnum-min (+ fixnum-bit  1) (+ fixnum-bit  2) 1)))

    (run-tests
     (lambda (a b c d)
       (string-append "fixnum-bit - 1 bits starting at " (number->string b)))
     (lambda (a b c d)
       (= (bit-extract a b c) d))
     (list
      (list fixnum-min 0 (+ fixnum-bit -1) (ash 0 (- fixnum-bit 1)))
      (list fixnum-min 1 (+ fixnum-bit  0) (ash 1 (- fixnum-bit 2)))
      (list fixnum-min 2 (+ fixnum-bit  1) (ash 3 (- fixnum-bit 3)))
      (list fixnum-min (+ fixnum-bit -2) (+ fixnum-bit fixnum-bit -3) (- (ash 1	(- fixnum-bit 1)) 2))
      (list fixnum-min (+ fixnum-bit -1) (+ fixnum-bit fixnum-bit -2) (- (ash 1	(- fixnum-bit 1)) 1))
      (list fixnum-min (+ fixnum-bit  0) (+ fixnum-bit fixnum-bit -1) (- (ash 1	(- fixnum-bit 1)) 1))
      (list fixnum-min (+ fixnum-bit  1) (+ fixnum-bit fixnum-bit  0) (- (ash 1	(- fixnum-bit 1)) 1))))

    (run-tests
     (lambda (a b c d)
       (string-append "fixnum-bit bits starting at " (number->string b)))
     (lambda (a b c d)
       (= (bit-extract a b c) d))
     (list
      (list fixnum-min 0 (+ fixnum-bit  0) (ash 1 (- fixnum-bit 1)))
      (list fixnum-min 1 (+ fixnum-bit  1) (ash 3 (- fixnum-bit 2)))
      (list fixnum-min 2 (+ fixnum-bit  2) (ash 7 (- fixnum-bit 3)))
      (list fixnum-min (+ fixnum-bit -2) (+ fixnum-bit fixnum-bit -2) (- (ash 1	fixnum-bit) 2))
      (list fixnum-min (+ fixnum-bit -1) (+ fixnum-bit fixnum-bit -1) (- (ash 1	fixnum-bit) 1))
      (list fixnum-min (+ fixnum-bit  0) (+ fixnum-bit fixnum-bit  0) (- (ash 1	fixnum-bit) 1))
      (list fixnum-min (+ fixnum-bit  1) (+ fixnum-bit fixnum-bit  1) (- (ash 1	fixnum-bit) 1))))

    (run-tests
     (lambda (a b c d)
       (string-append "fixnum-bit + 1 bits starting at " (number->string b)))
     (lambda (a b c d)
       (= (bit-extract a b c) d))
     (list
      (list fixnum-min 0 (+ fixnum-bit  1) (ash  3 (- fixnum-bit 1)))
      (list fixnum-min 1 (+ fixnum-bit  2) (ash  7 (- fixnum-bit 2)))
      (list fixnum-min 2 (+ fixnum-bit  3) (ash 15 (- fixnum-bit 3)))
      (list fixnum-min (+ fixnum-bit -2) (+ fixnum-bit fixnum-bit -1) (- (ash 1 (+ fixnum-bit 1)) 2))
      (list fixnum-min (+ fixnum-bit -1) (+ fixnum-bit fixnum-bit  0) (- (ash 1 (+ fixnum-bit 1)) 1))
      (list fixnum-min (+ fixnum-bit  0) (+ fixnum-bit fixnum-bit  1) (- (ash 1 (+ fixnum-bit 1)) 1))
      (list fixnum-min (+ fixnum-bit  1) (+ fixnum-bit fixnum-bit  2) (- (ash 1 (+ fixnum-bit 1)) 1)))))

  (with-test-prefix "extract from fixnum-min - 1"

    (run-tests
     (lambda (a b c d)
       (string-append "single bit " (number->string b)))
     (lambda (a b c d)
       (= (bit-extract a b c) d))
     (list
      (list (- fixnum-min 1) 0 1 1)
      (list (- fixnum-min 1) 1 2 1)
      (list (- fixnum-min 1) (+ fixnum-bit -2) (+ fixnum-bit -1) 1)
      (list (- fixnum-min 1) (+ fixnum-bit -1) (+ fixnum-bit  0) 0)
      (list (- fixnum-min 1) (+ fixnum-bit  0) (+ fixnum-bit  1) 1)
      (list (- fixnum-min 1) (+ fixnum-bit  1) (+ fixnum-bit  2) 1)))

    (run-tests
     (lambda (a b c d)
       (string-append "fixnum-bit - 1 bits starting at " (number->string b)))
     (lambda (a b c d)
       (= (bit-extract a b c) d))
     (list
      (list (- fixnum-min 1) 0 (+ fixnum-bit -1) (- (ash 1 (- fixnum-bit 1)) 1 (ash 0 (- fixnum-bit 1))))
      (list (- fixnum-min 1) 1 (+ fixnum-bit  0) (- (ash 1 (- fixnum-bit 1)) 1 (ash 1 (- fixnum-bit 2))))
      (list (- fixnum-min 1) 2 (+ fixnum-bit  1) (- (ash 1 (- fixnum-bit 1)) 1 (ash 1 (- fixnum-bit 3))))
      (list (- fixnum-min 1) (+ fixnum-bit -2) (+ fixnum-bit fixnum-bit -3) (- (ash 1 (- fixnum-bit 1)) 3))
      (list (- fixnum-min 1) (+ fixnum-bit -1) (+ fixnum-bit fixnum-bit -2) (- (ash 1 (- fixnum-bit 1)) 2))
      (list (- fixnum-min 1) (+ fixnum-bit  0) (+ fixnum-bit fixnum-bit -1) (- (ash 1 (- fixnum-bit 1)) 1))
      (list (- fixnum-min 1) (+ fixnum-bit  1) (+ fixnum-bit fixnum-bit  0) (- (ash 1 (- fixnum-bit 1)) 1))))

    (run-tests
     (lambda (a b c d)
       (string-append "fixnum-bit bits starting at " (number->string b)))
     (lambda (a b c d)
       (= (bit-extract a b c) d))
     (list
      (list (- fixnum-min 1) 0 (+ fixnum-bit  0) (- (ash 1 fixnum-bit) 1 (ash 1 (- fixnum-bit 1))))
      (list (- fixnum-min 1) 1 (+ fixnum-bit  1) (- (ash 1 fixnum-bit) 1 (ash 1 (- fixnum-bit 2))))
      (list (- fixnum-min 1) 2 (+ fixnum-bit  2) (- (ash 1 fixnum-bit) 1 (ash 1 (- fixnum-bit 3))))
      (list (- fixnum-min 1) (+ fixnum-bit -2) (+ fixnum-bit fixnum-bit -2) (- (ash 1 fixnum-bit) 3))
      (list (- fixnum-min 1) (+ fixnum-bit -1) (+ fixnum-bit fixnum-bit -1) (- (ash 1 fixnum-bit) 2))
      (list (- fixnum-min 1) (+ fixnum-bit  0) (+ fixnum-bit fixnum-bit  0) (- (ash 1 fixnum-bit) 1))
      (list (- fixnum-min 1) (+ fixnum-bit  1) (+ fixnum-bit fixnum-bit  1) (- (ash 1 fixnum-bit) 1))))

    (run-tests
     (lambda (a b c d)
       (string-append "fixnum-bit + 1 bits starting at " (number->string b)))
     (lambda (a b c d)
       (= (bit-extract a b c) d))
     (list
      (list (- fixnum-min 1) 0 (+ fixnum-bit  1) (- (ash 1 (+ fixnum-bit 1)) 1 (ash 1 (- fixnum-bit 1))))
      (list (- fixnum-min 1) 1 (+ fixnum-bit  2) (- (ash 1 (+ fixnum-bit 1)) 1 (ash 1 (- fixnum-bit 2))))
      (list (- fixnum-min 1) 2 (+ fixnum-bit  3) (- (ash 1 (+ fixnum-bit 1)) 1 (ash 1 (- fixnum-bit 3))))
      (list (- fixnum-min 1) (+ fixnum-bit -2) (+ fixnum-bit fixnum-bit -1) (- (ash 1 (+ fixnum-bit 1)) 3))
      (list (- fixnum-min 1) (+ fixnum-bit -1) (+ fixnum-bit fixnum-bit  0) (- (ash 1 (+ fixnum-bit 1)) 2))
      (list (- fixnum-min 1) (+ fixnum-bit  0) (+ fixnum-bit fixnum-bit  1) (- (ash 1 (+ fixnum-bit 1)) 1))
      (list (- fixnum-min 1) (+ fixnum-bit  1) (+ fixnum-bit fixnum-bit  2) (- (ash 1 (+ fixnum-bit 1)) 1))))))
